'''
Created on Mar 23, 2013

@author: Petko
'''
from django.views.generic.base import View
from sent.utils import CommonResult, ErrorIds, createValidToken, AccessToken
from sent.utils.result_printer import getPrinter

from sent.model.OpinionDBManager import OpinionDBManager
from sent.utils.permissions import parseScopeList
from sent.model.entities.opinion import OpinionState

class GetPathToOpinion(View):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        pass
    
    def get(self, *args, **kwargs):
        
        cResult = CommonResult()
        
        accessTokenStr = self.request.GET.get('access_token', None)
        parent_op = self.request.GET.get('parent_op', None)
        child_op = self.request.GET.get('child_op', None)
        
        try:
            max_content_length = int(self.request.GET.get('max_content_length', 0))
        except ValueError:
            cResult.errorId = ErrorIds.BADREQUEST
            cResult.result = 'Bad format of max_content_length'
            return getPrinter().printResult(cResult)
        
        if parent_op and child_op:
            
            dbMan = OpinionDBManager(True)
            try:
                parentOp = dbMan.get_opinion_by_id_str(parent_op, allowed_states=[OpinionState.NORMAL, OpinionState.WAITING_UPDATE])
                childOp = dbMan.get_opinion_by_id_str(child_op, allowed_states=[OpinionState.NORMAL, OpinionState.WAITING_UPDATE])
                
                if parentOp and childOp:
                    if parentOp.base_op_id == childOp.base_op_id:
                        if parentOp.nested_set_left < childOp.nested_set_left and parentOp.nested_set_right > childOp.nested_set_right:
                            if accessTokenStr:
                                accessToken, error = createValidToken(accessTokenStr)
                            else:
                                if parentOp.containsDefaultScope('r'):
                                    accessToken = AccessToken()
                                    accessToken.scopesSet = parseScopeList('all:r')
                                else:
                                    error = "Missing 'access_token' parameter"
                            
                            if accessToken:
                                cResult.errorId = ErrorIds.SUCCESS
                                path = dbMan.getPathFromOpToOp(parentOp, childOp)
                                dbMan.fillOpinionTexts(path, max_content_length)
                                
                                cResult.result = [op.toPrimitiveDict() for op in path]
                            else:
                                cResult.errorId = ErrorIds.UNAUTHORIZED
                                cResult.result = error
                                
                        else:
                            cResult.errorId = ErrorIds.BADREQUEST
                            cResult.result = "opinion %s is not parent of %s" % (parentOp.getUUID(), childOp.getUUID())
                    else:
                        cResult.errorId = ErrorIds.BADREQUEST
                        cResult.result = "Base opinion Id is different: %s / %s" % (parentOp.getBaseUUID(), childOp.getBaseUUID())
                else:
                    cResult.errorId = ErrorIds.NOT_FOUND
                    cResult.result = "Opinion with ID "
                    if not parentOp:
                        cResult.result += parent_op
                    else:
                        cResult.result += child_op
                    cResult.result += ' not found' 
            finally:
                dbMan.closeSession()
        else:
            cResult.errorId = ErrorIds.BADREQUEST
            cResult.result = "Missing parameter"
            
        return getPrinter().printResult(cResult)
    
    